/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mediaideas.videoengine;

import android.app.Activity;
import android.graphics.Color;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;

import com.mediaideas.videoengine.constants.AppMessageDef;
import com.mediaideas.videoengine.utils.VideoEngineException;
import com.mediaideas.videoengine.utils.VideoEngineUtils;

import java.lang.ref.WeakReference;

public class VideoEngineActivity extends Activity {
    protected final static String TAG = "libVideoEngine";
    protected Handler mHandler;
    protected AudioProc mAudioProc;
    protected TextView mProcessTv;

    protected class MsgHandler extends Handler {
        WeakReference<VideoEngineActivity> reference;
        public MsgHandler(VideoEngineActivity activity) {
            reference = new WeakReference<VideoEngineActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            VideoEngineActivity activity = reference.get();
            if (activity == null) {
                return;
            }
            switch (msg.what) {
                case AppMessageDef.MSG_JNI_CALLBACK_NOTIFY_PROGRESS:
                    activity.mProcessTv.setText("Progress:" + String.valueOf(msg.arg1));
                    break;
            }

            super.handleMessage(msg);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        mProcessTv = (TextView) findViewById(R.id.progress_tv);
        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        final AudioMixerParams audioMixerParams = new AudioMixerParams();
        Log.e(TAG, "input_one.mp3=" + VideoEngineUtils.isFileExists(dirPath + "/input_one.mp3"));
        audioMixerParams.setSrcAudioFilePath(dirPath + "/input_one.mp3");
        audioMixerParams.setBackgroundAudioFilePath(dirPath + "/input_two.mp3");
        try {
            audioMixerParams.setDstAudioFilePath(dirPath + "/output.aac");
        } catch (VideoEngineException e) {
            e.printStackTrace();
        }
        audioMixerParams.setSrcAudioStartTime(0);
        audioMixerParams.setSrcAudioVolume(1.0F);
        audioMixerParams.setBackgroundAudioVolume(0.5F);
        audioMixerParams.setDstAudioSampleRate(48000);
        audioMixerParams.setDstAudioBitRate(128);
        audioMixerParams.setAudioMixerDuration(1000000);
        mHandler = new MsgHandler(this);
        mAudioProc = new AudioProc(this, mHandler);

        final View startConvertBtn = findViewById(R.id.start_convert_btn);
        startConvertBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startConvertBtn.setClickable(false);
                startConvertBtn.setBackgroundColor(Color.RED);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        mAudioProc.startConvertAudio(audioMixerParams);
                    }
                }).start();
            }
        });

        View startRecorder = findViewById(R.id.start_recorder_btn);
        View stopRecorder = findViewById(R.id.stop_recorder_btn);

        startRecorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                try {
//                    if (mAudioCapture == null) {
//                        mAudioCapture = new AudioCapture(VideoEngineActivity.this, FileUtils.getAudioFile(VideoEngineActivity.this).getAbsolutePath());
//                        mAudioCapture.start();
//                    }
//                } catch (XYJException e) {
//                    e.printStackTrace();
//                }
            }
        });

        stopRecorder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (mAudioCapture != null) {
//                    mAudioCapture.stop();
//                    mAudioCapture = null;
//                }
            }
        });
    }
}
